package org.example.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSessionBindingListener;
import java.time.LocalDateTime;
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Autowired
    private HttpServletRequest request;
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createTime",LocalDateTime.now());
//        metaObject.setValue("createUser",(Long)request.getSession().getAttribute("employee"));
//        metaObject.setValue("updateUser",(Long)request.getSession().getAttribute("employee"));
        metaObject.setValue("createUser",BaseContxt.getId());
        metaObject.setValue("updateUser",BaseContxt.getId());

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser",((Long)request.getSession().getAttribute("employee") == null ? (Long)request.getSession().getAttribute("user") : (Long)request.getSession().getAttribute("employee")));
    }
}
